package org.pih.migration.load.objects;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Hashtable;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class XFormBuilder {

	protected Hashtable<String, Step> stepTable;
	
	public void build(Protocol protocol) {
		
		System.out.println("Building XForm for " + protocol.getName());
		this.stepTable = protocol.getStepTable();
		
		Namespace namespaceDefault = Namespace.getNamespace("http://www.w3.org/2002/xforms");
		Namespace namespaceH = Namespace.getNamespace("h", "http://www.w3.org/1999/xhtml");
		Namespace namespaceEv = Namespace.getNamespace("ev", "http://www.w3.org/2001/xml-events");
		Namespace namespaceXsd = Namespace.getNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
		Namespace namespaceJr = Namespace.getNamespace("jr", "http://openrosa.org/javarosa");
		
		Document document = new Document();
		Element root = new Element("html", namespaceH);
		root.addNamespaceDeclaration(namespaceDefault);
		root.addNamespaceDeclaration(namespaceEv);
		root.addNamespaceDeclaration(namespaceXsd);
		root.addNamespaceDeclaration(namespaceJr);
		
		document.setRootElement(root);
		
		Element head = new Element("head", namespaceH);
		root.addContent(head);
		
		Element title = new Element("title", namespaceH);
		title.setText(protocol.getName());
		head.addContent(title);
		
		Element model = new Element("model");
		head.addContent(model);
		
		Element instance = new Element("instance");
		model.addContent(instance);
		
		Element data = new Element("adultDosage");
		instance.addContent(data);
		
		data.addContent(new Element("form_version").setText("0.0.1"));

		Element body = new Element("body", namespaceH);
		root.addContent(body);
		
		HashMap<String, Integer> keyMap = new HashMap<String, Integer>();
		
		Step step = protocol.getStepTable().get("A");
//		Step step = protocol.getStepTable().get("C1");
		step.printControlTree(null, body, model, data, keyMap, protocol.getStepTable());
		
		Element done = new Element("select1");
		done.setAttribute("ref", "DONE");
		Element label = new Element("label").setText("Done");
		done.addContent(label);
		body.addContent(done);
		Element item = new Element("item");
		item.addContent(new Element("label").setText("Ok"));
		item.addContent(new Element("value").setText("ok"));
		done.addContent(item);
		data.addContent(new Element("DONE"));
		
		StringWriter sw = new StringWriter();
		XMLOutputter outputter = null;
		
		try {
			Format format = Format.getCompactFormat();
			format.setIndent(" ");
			format.setEncoding("UTF-8");
			format.setOmitEncoding(true);
			outputter = new XMLOutputter(format);
			outputter.output(document, sw);
//			System.out.println("XML: " + sw.getBuffer().toString());
			
			FileWriter fstream = new FileWriter("testOutput.xml");
		    BufferedWriter out = new BufferedWriter(fstream);
		    out.write(sw.getBuffer().toString());
		    out.close();
		    fstream.close();
		    System.out.println("done.");
		} catch (Exception e) {
			e.printStackTrace();
		}

		
	}
}
